import {timeago} from '../../util/util.js'
const app = getApp()
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: "",
    nickName: "",
    title: "",
    photo: [],
    newTime: "",
    time: "",
    myid: "",
    cid: "",
    _id: "",
    i: "",
    collected: false
  },

  //放大图片
  onViewPost(){
    wx.previewImage({
      urls: [...this.data.photo],
    })
  },

  //点击收藏
  onCollection(){
    //判断用户是否处在登录状态
    if(app.userInfo._id){

      // 建立收藏夹
      const collect = {avatar: this.data.avatar, collected: true, nickName:this.data.  nickName, title: this.data.title, photo:this.data.photo, txt: this.data.txt, _id: this.data._id, time: this.data.time}
      // 判断收藏是否为false
      if(!this.data.collected){
        // 没有收藏时  
            db.collection('news').doc(app.userInfo._id).update({
              data:{
                collect: _.unshift(collect)
              }
            }).then((res)=>{
              console.log(res)
              this.setData({
                collected: true
              })
              db.collection('news').doc(app.userInfo._id).field({
                collect: true
              }).get().then((res)=>{
                console.log(res)
                app.userInfo.collect = res.data.collect
              })
              wx.showToast({
                title: '收藏成功',
              })
            })
      }else{
        // 已经收藏时
        db.collection('news').doc(app.userInfo._id).field({
          collect: true
        }).get().then((res)=>{
          console.log(res)
          console.log(collect)
          const collects = res.data.collect
          // findIndex查找到collects中满足条件的指定对象的索引值
          const cid = collects.findIndex(item => item.txt==collect.txt && item.  nickName==collect.nickName)
          // 将原收藏数据中查找到的数据删除掉
          collects.splice(cid,1)
          //更新收藏列表
          db.collection('news').doc(app.userInfo._id).update({
            data: {
              collect: collects
            },
            success: (res)=>{
              app.userInfo.collect = collects
              this.setData({
                collected: false
              })
            }
            
          })
          wx.showToast({
            title: '取消收藏',
          })
        })
      }
    }else{
      //未登录状态
      wx.showToast({
        title: '请先登录',
        duration: 2000,  //提示时间
      })
      setTimeout(()=>{
        wx.switchTab({
          url: '/pages/mine/mine',
        })
      },2000)
    }
  },

  //跳转个人主页
  onPerdetail(){
      wx.navigateTo({
        url: '/pages/personal-detail/personal-detail?myid=' + this.data.myid,
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    const id = options.id
    const _id = options._id
    const myid = options.myid
    const time = options.time
    //条件判断到底是从什么途径点进的detail页面
    if(id){
      //如果id和_id都存在，代表从个人主页进入
      if(_id){
        db.collection('news').where({
          _id: _id
        }).field({
          avatar: true,
          nickName: true,
          content: true
        }).get().then((res)=>{
          const newTime = timeago(res.data[0].content[id].time)
          this.setData({
            avatar: res.data[0].avatar,
            nickName: res.data[0].nickName,
            photo: res.data[0].content[id].photo,
            title:  res.data[0].content[id].title,
            txt:  res.data[0].content[id].txt,
            _id: _id,
            myid: _id,
            time,
            newTime
          })
          
        })
        this.isCollect()
      }else{
        //如果只有id，代表从我的发布页面进入
        const content = app.userInfo.content[id]
        this.setData({
          time
        })
        this.bind(content)
        this.isCollect()
      }
    }else{
      //如果只有myid，代表从首页直接进入
      console.log(111)
      console.log(options)
      db.collection('news').where({
        _id: myid
      }).field({
        avatar: true,
        nickName: true,
        content: true
      }).get().then((res)=>{
        const newTime = timeago(res.data[0].content[0].time)
        this.setData({
          avatar: res.data[0].avatar,
          nickName: res.data[0].nickName,
          photo: res.data[0].content[0].photo,
          title:  res.data[0].content[0].title,
          txt:  res.data[0].content[0].txt,
          _id: myid,
          myid,
          time,
          newTime
        })
        this.isCollect()
      })
    }
  },

  //判断文章是否已经收藏
  isCollect(){
    db.collection('news').doc(app.userInfo._id).field({
      collect: true
    }).get().then((res)=>{
      console.log(res)
      const cid = res.data.collect.findIndex(item => item.txt==this.data.txt && 
    item.nickName==this.data.nickName)
      if(cid != -1){
        this.setData({
          collected: true,
          // i: cid,
          // cid
        })
      }
    })       
  },

  //通过时间来确定具体的收藏文章所在位置

  //绑定详情内容
  bind(content){
    const newTime = timeago(content.time)
    this.setData({
      avatar: app.userInfo.avatar,
      nickName: app.userInfo.nickName,
      _id: app.userInfo._id,
      title: content.title,
      txt: content.txt,
      photo: content.photo,
      newTime
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})